<?php

/**
 * @file
 * Definition of example_handler_custom_field
 */

/**
 * Provides a custom views field.
 */
class jessica_stylebook_handler_looking_for_more_buy_link extends views_handler_field {
    function option_definition() {
        $options = parent::option_definition();
        return $options;
    }

    function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
    }

    function query() {
        // do nothing -- to override the parent query.
    }

    function render($data) {
        $items = (!empty($data->field_field_item_buy_link)) ? $data->field_field_item_buy_link : null;
        $link = '';



        if (count($items) > 0) {
            foreach ($items as $key => $item) {
                //dpm($item, 'item');

                if ($item['raw']['safe_value']) {
                    $link .= '<a class="style-item-buy" href="' . $item['raw']['safe_value'] . '">';

                    /*$shop =  $data->field_field_item_shop_name;

                    if (!empty($shop)) {
                        $link .= 'Buy at ' . $shop[0]['raw']['safe_value'];
                    } else {
                        $link .= 'Buy';
                    }*/

                    $link .= 'BUY this item';

                    $link .= '</a>';
                }
            }
        } else {
            if (isset($data->node_field_data_field_styling_item_id_nid)) {
                $path = base_path() . drupal_get_path_alias('node/'.$data->node_field_data_field_styling_item_id_nid);
            } else {
                $path = base_path() . drupal_get_path_alias('node/'.$data->nid);
            }


            return '<a class="style-item-detail" href="' . $path . '">See item detail</a>';

            //style-item-detail

        }

        return $link;
    }
}

